package com.gxa.weixing.pc.service.impl;

import com.gxa.weixing.core.common.ExceptionCode;
import com.gxa.weixing.core.exception.BusinessException;
import com.gxa.weixing.core.pojo.ForGoods;
import com.gxa.weixing.core.pojo.ForGoodsDetails;
import com.gxa.weixing.core.service.impl.BaseServiceImpl;
import com.gxa.weixing.pc.mapper.ForGoodsDetailsMapper;
import com.gxa.weixing.pc.mapper.ForGoodsMapper;
import com.gxa.weixing.pc.service.ForGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
@Transactional(rollbackFor = Throwable.class)
public class ForGoodsServiceImpl extends BaseServiceImpl<ForGoods> implements ForGoodsService {

    @Autowired
    private ForGoodsMapper forGoodsMapper = null;

    @Autowired
    private ForGoodsDetailsMapper forGoodsDetailsMapper = null;

    @Override
    public List<Map<String,Object>> getAllDetailsByPage(Map<String, Object> map) throws BusinessException {
        Map<String, Object> checkKey = checkKey(map);
        Integer page = null;
        Integer size = null;
        if (checkKey.get("page") == null || (Integer)checkKey.get("page") < 1) {
            page = 1;
        } else {
            page = (Integer) checkKey.get("page");
        }
        if (checkKey.get("size") == null || (Integer)checkKey.get("size") < 1) {
            size = 5;
        } else {
            size = (Integer) checkKey.get("size");
        }
        checkKey.put("offset",(page - 1) * size);
        checkKey.put("limit",size);
        List<Map<String, Object>> detailsByPage = forGoodsMapper.getDetailsByPage(checkKey);
        if (detailsByPage == null || detailsByPage.size() == 0) {
            throw new BusinessException(ExceptionCode.NO_DATE);
        }
        return detailsByPage;
    }

    @Override
    public Integer getAllDetailsByPageCount(Map<String, Object> map) throws BusinessException {
        Map<String, Object> checkKey = checkKey(map);
        checkKey.remove("offset");
        checkKey.remove("limit");
        return forGoodsMapper.getDetailsByPage(checkKey).size();
    }

    @Override
    public void deleteForGoods(Long id) throws BusinessException {
        if (id == null || id <= 0) {
            throw new BusinessException(ExceptionCode.PARAM_ERROR);
        }
        ForGoods forGoods = forGoodsMapper.selectByPrimaryKey(id);
        if (forGoods == null) {
            throw new BusinessException(ExceptionCode.OPERATION_EXCEPTION);
        }
        int i = forGoodsMapper.deleteByPrimaryKey(id);
        if (i < 1) {
            throw new BusinessException(ExceptionCode.OPERATION_FAILURE);
        }
        Example example = new Example(ForGoodsDetails.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("forGoodsId",id);
        int i1 = forGoodsDetailsMapper.deleteByExample(example);
        if (i1 < 1) {
            throw new BusinessException(ExceptionCode.OPERATION_FAILURE);
        }
    }

    @Override
    public void saveForGoods(ForGoodsDetails detail) throws BusinessException {
        //判断是否接收到参数
        if (detail == null || detail.getForGoods() == null) {
            throw new BusinessException(ExceptionCode.PARAM_ERROR);
        }

        if (!"红包".equals(detail.getForGoods().getForGoodsType())) {
            detail.getForGoods().setMoney(null);
        }
        //判断是增加还是修改
        if (detail.getForGoodsDetailsId() == null || detail.getForGoods().getForGoodsId() == null) {
            //新增
            ForGoods forGoods = detail.getForGoods();
            int i = forGoodsMapper.insert(forGoods);
            if (i < 1) {
                throw new BusinessException(ExceptionCode.OPERATION_FAILURE);
            }
            detail.setForGoodsId(forGoods.getForGoodsId());
            int k = forGoodsDetailsMapper.insert(detail);
            if (k < 1) {
                throw new BusinessException(ExceptionCode.OPERATION_FAILURE);
            }
        } else {
            //修改
            int i = forGoodsMapper.updateByPrimaryKey(detail.getForGoods());
            if (i < 1) {
                throw new BusinessException(ExceptionCode.OPERATION_FAILURE);
            }
            int k = forGoodsDetailsMapper.updateByPrimaryKey(detail);
            if (k < 1) {
                throw new BusinessException(ExceptionCode.OPERATION_FAILURE);
            }
        }
    }

    private Map<String,Object> checkKey(Map<String,Object> map) {
        Set<String> keySet = map.keySet();
        HashMap<String, Object> hashMap = new HashMap<>();
        if (keySet.size() != 0) {
            for (String s : keySet) {
                Object o = map.get(s);
                if (!StringUtils.isEmpty(o)) {
                    hashMap.put(s,o);
                }
            }
        }
        return hashMap;
    }
}
